package com.ruoyi.gis.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gis.domain.GisLayerGroupProductVo;
import com.ruoyi.gis.domain.GisLayerProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gis.mapper.GisLayerProductMapper;
import com.ruoyi.gis.domain.GisLayerProduct;
import com.ruoyi.gis.service.IGisLayerProductService;

/**
 * layerProductService业务层处理
 *
 * @author rww
 * @date 2023-11-16
 */
@Service
public class GisLayerProductServiceImpl implements IGisLayerProductService
{
    @Autowired
    private GisLayerProductMapper gisLayerProductMapper;

    /**
     * 查询layerProduct
     *
     * @param id layerProduct主键
     * @return layerProduct
     */
    @Override
    public GisLayerProduct selectGisLayerProductById(Long id)
    {
        return gisLayerProductMapper.selectGisLayerProductById(id);
    }

    /**
     * 查询layerProduct列表
     *
     * @param gisLayerProduct layerProduct
     * @return layerProduct
     */
    @Override
    public List<GisLayerProduct> selectGisLayerProductList(GisLayerProduct gisLayerProduct)
    {
        return gisLayerProductMapper.selectGisLayerProductList(gisLayerProduct);
    }

    /**
     * 新增layerProduct
     *
     * @param gisLayerProduct layerProduct
     * @return 结果
     */
    @Override
    public int insertGisLayerProduct(GisLayerProduct gisLayerProduct)
    {
        gisLayerProduct.setCreateTime(DateUtils.getNowDate());
        return gisLayerProductMapper.insertGisLayerProduct(gisLayerProduct);
    }

    /**
     * 修改layerProduct
     *
     * @param gisLayerProduct layerProduct
     * @return 结果
     */
    @Override
    public int updateGisLayerProduct(GisLayerProduct gisLayerProduct)
    {
        gisLayerProduct.setUpdateTime(DateUtils.getNowDate());
        return gisLayerProductMapper.updateGisLayerProduct(gisLayerProduct);
    }

    /**
     * 批量删除layerProduct
     *
     * @param ids 需要删除的layerProduct主键
     * @return 结果
     */
    @Override
    public int deleteGisLayerProductByIds(Long[] ids)
    {
        return gisLayerProductMapper.deleteGisLayerProductByIds(ids);
    }

    /**
     * 删除layerProduct信息
     *
     * @param id layerProduct主键
     * @return 结果
     */
    @Override
    public int deleteGisLayerProductById(Long id)
    {
        return gisLayerProductMapper.deleteGisLayerProductById(id);
    }

    @Override
    public void addGroupProduct(Long userId, GisLayerGroupProductVo gisLayerGroupProductVo) {
        GisLayerProduct  product = new GisLayerProduct();
        product.setUserId(userId);
        product.setName(gisLayerGroupProductVo.getName());
        product.setType(gisLayerGroupProductVo.getType());
        product.setParentId(gisLayerGroupProductVo.getParentId());
        gisLayerProductMapper.insertGisLayerProduct(product);
    }

    @Override
    public void addProduct(Long loginUser,
                           GisLayerProductVo gisLayerProductVo) {
        Date nowDate = DateUtils.getNowDate();
        gisLayerProductVo.getLayers().stream().forEach(item->{
            GisLayerProduct  product = new GisLayerProduct();
            product.setGroupId(gisLayerProductVo.getGroupId());
            product.setLayerId(item.getLayerId());
            product.setUserId(loginUser);
            product.setType(gisLayerProductVo.getType());
            product.setParentId(gisLayerProductVo.getParentId());
            List<GisLayerProduct> gisLayerProducts = gisLayerProductMapper.
                    selectGisLayerProductList(product);
//            if(gisLayerProducts==null||gisLayerProducts.size()==0){
//                product.setName(item.getName());
//                product.setCreateTime(nowDate);
//                gisLayerProductMapper.insertGisLayerProduct(product);
//            }
            product.setName(item.getName());
            product.setCreateTime(nowDate);
            gisLayerProductMapper.insertGisLayerProduct(product);
        });
    }

    @Override
    public int updateGisLayerProductSort(List<GisLayerProduct> products) {
        for(int i=0;i<products.size();i++){
            GisLayerProduct product = products.get(i);
            product.setSort(i);
            gisLayerProductMapper.updateGisLayerProduct(product);
        }
        return products.size();
    }


}
